package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.ExcelSubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.XXHash32;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author huangqicai
 * @since 2022-10-19
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    /*后端读取到excel文件里面的内容后，将数据保存到数据库里面*/
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
        try {
            /*1.获取写入流*/
            InputStream inputStream = file.getInputStream();
            /*2.使用EasyExcel进行操作解析*/
            EasyExcel.read(inputStream, ExcelSubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*查询所有课程，形成树类结构*/
    @Override
    public R getAllSubject() {
        /*1.获取一级分类的所有集合*/
        QueryWrapper<EduSubject> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("parent_id", 0);
        List<EduSubject> oneSubjects = baseMapper.selectList(wrapper1);
        /*2.获取二级分类的所有集合*/
        QueryWrapper<EduSubject> wrapper2 = new QueryWrapper<>();
        wrapper2.ne("parent_id", 0);
        List<EduSubject> twoSubjects = baseMapper.selectList(wrapper2);
        ArrayList<OneSubject> finalList = new ArrayList<>();
        /*3.遍历一级分类，在遍历一级分类的同时，得到oneSubject，将twoSubject放到oneSubject里面*/
        for(int i=0;i<oneSubjects.size();i++){
            EduSubject subject1 = oneSubjects.get(i);
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(subject1,oneSubject);
            /*遍历二级分类*/
            /*注意这个集合的位置*/
            ArrayList<TwoSubject> twoSubjects2 = new ArrayList<>();
            for(int j=0;j<twoSubjects.size();j++){
                EduSubject subject2 = twoSubjects.get(j);
                if(subject2.getParentId().equals(subject1.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(subject2,twoSubject);
                    twoSubjects2.add(twoSubject);
                }
            }
            /*注意这个集合的位置*/
            oneSubject.setChildren(twoSubjects2);
            finalList.add(oneSubject);
        }
        if(finalList.isEmpty()){
            return R.error();
        }
        return R.ok().data("list", finalList);
    }

}
